<?php

class imagr {
	
	function loadImage($imagem=false) {
		if ($imagem) {
			if (substr($imagem, -3)=="peg" || substr($imagem, -3)=="jpg") {
				$img = imagecreatefromjpeg($imagem);
			} else if (substr($imagem, -3)=="png") {
				$img = imagecreatefrompng($imagem);
			} else if (substr($imagem, -3)=="gif") {
				$img = imagecreatefromgif($imagem);
			}

			return $img;
		} else {
			return false;
		}
	}
	
	

	function recorta($img, $x, $y, $w, $h) {
		$image = imagecreatetruecolor($w, $h);
		$wo = imagesx($img);
		$ho = imagesy($img);
		imagecopyresampled($image, $img, -$x, -$y, 0, 0, $wo, $ho, $wo, $ho);
		return $image;
	}


	function redimensiona($img, $w, $h) {
		$image = imagecreatetruecolor($w, $h);
		$wo = imagesx($img);
		$ho = imagesy($img);
		imagecopyresampled($image, $img, 0, 0, 0, 0, $w, $h, $wo, $ho);
 
		return $image;
	}

	function girar($img, $a, $bg=-1) {
		$img = imagerotate($img, $a, $bg);
		imagealphablending($img, false);
		imagesavealpha($img, true);
		return $img;
	}

	function logomarca($jpegImg, $wmImg, $w=100, $h=100, $t=0, $l=0, $a=100) {

		$wm = imagecreatetruecolor($w, $h);
		$wmImg = imagecopyresampled($wm, $wmImg, 0, 0, 0, 0, $w, $h, imageSX($wmImg), imageSY($wmImg));
		imageCopyMerge($jpegImg, $wm, $t, $l, 0, 0, imageSX($wm), imageSY($wm), $a);
		imagealphablending($wm, false);
		imagesavealpha($wm, true);
		return $jpegImg;

	}

	function flip($img, $direcao="vertical") {

		$wo = imagesx($img);
		$ho = imagesy($img);
		$image = imagecreatetruecolor($wo, $ho);

		if ($direcao=="vertical") {

			$dst_x = 0;
			$dst_y = 0;
			$src_x = 0;
			$src_y = $ho - 1;
			$dst_w = $wo;
			$dst_h = $ho;
			$src_w = $wo;
			$src_h = 0 - $ho;
			imagecopyresampled($image, $img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);

		} else if ($direcao=="horizontal") {

			$dst_x = 0;
			$dst_y = 0;
			$src_x = $wo -1;
			$src_y = 0;
			$dst_w = $wo;
			$dst_h = $ho;
			$src_w = 0 - $wo;
			$src_h = $ho;
			imagecopyresampled($image, $img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);


		} else {

			$image = $img;

		}
		
		imagealphablending($image, false);
		imagesavealpha($image, true);
			
		return $image;
	}


	function contraste($img, $percento){
		$x = imagesx($img);
		$y = imagesy($img);
		$dest = imagecreatetruecolor($x, $y);

		$percento = $percento / 100;
		for($i=0; $i<$x; $i++){
			for($j=0; $j<$y; $j++){
				$rgb = imagecolorat($img, $i, $j);
				$r = ($rgb >> 16) & 0xFF;
				$g = ($rgb >> 8) & 0xFF;
				$b = $rgb & 0xFF;
				$r = $percento > 1 ? min(floor($r * $percento), 255) : max(ceil($r * $percento), 0);
				$g = $percento > 1 ? min(floor($g * $percento), 255) : max(ceil($g * $percento), 0);
				$b = $percento > 1 ? min(floor($b * $percento), 255) : max(ceil($b * $percento), 0);
				imagesetpixel($dest, $i, $j, imagecolorallocate($dest, $r, $g, $b));
			}
		}
		imagealphablending($dest, false);
		imagesavealpha($dest, true);
		return $dest;
	}
	
	function negativo($img) {
		imagefilter($img, IMG_FILTER_NEGATE);
		return $img;
	}
	
	function grayscale($img) {
		imagefilter($img, IMG_FILTER_GRAYSCALE);
		return $img;
	}
	
	function texto($img, $texto, $fonte="fontes/verdana.ttf", $cor, $tamanho=10, $top=0, $left=0) {
		$textcolor = imagecolorallocate($img, $cor[0], $cor[1], $cor[2]);
		imagettftext($img, $tamanho, 0, $left, $top, $textcolor, $fonte, $texto);
		return $img;
	}
	
	function colorir($img, $param) {
		$r = $param["r"];
		$g = $param["g"];
		$b = $param["b"];
		imagefilter($img, IMG_FILTER_COLORIZE, $r, $g, $b);
		return $img;
	}


}

?>